package com.project.projectdemo.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class HttpClientUtils {
	static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
	public static final String XMLTYPE = "XML";
	public static final String JSONTYPE = "JSON";
	public static final String POST = "POST";
	public static final String DELETE = "DELETE";
	public static final String GET = "GET";
	
	/*
	 * JSON类型POST请求
	 * 
	 */
	public static String doPostJSON(String requestUrl,String param){
		return doPost(requestUrl,param,JSONTYPE);
	}
	
	public String doPostXML(String requestUrl,String param){
		return doPost(requestUrl,param,XMLTYPE);
	}
	
	public static String deleteJSON(String requestUrl,String param){
		return todo(requestUrl,param,JSONTYPE,DELETE);
	}
	
	public String deleteXML(String requestUrl,String param){
		return todo(requestUrl,param,XMLTYPE,DELETE);
	}
	
	private static String doPost(String requestUrl,String param,String type){
		HttpURLConnection conn = null;
		URL url = null;
		InputStream in = null;
		BufferedReader reader = null;
		OutputStream out = null;
		StringBuffer buffer = new StringBuffer();
		try {
			url = new URL(requestUrl);
			conn = (HttpURLConnection) url.openConnection();
			//指可以从这个连接中读取数据，默认为true
			conn.setDoInput(true);
			//指可以往这个连接写入数据，往服务器发送数据，默认为false
			conn.setDoOutput(true);
			//请求方法
			conn.setRequestMethod("POST");
			//连接超时时间 设置5分钟
			conn.setConnectTimeout(5*60*1000);
			//读取超时时间 设置5分钟
			conn.setReadTimeout(5*60*1000);
			//参数类型
			if(XMLTYPE.equals(type)){
				conn.addRequestProperty("Content-Type", "text/xml");
			}else if(JSONTYPE.equals(type)){
				conn.addRequestProperty("Content-Type", "application/json");
			}
			
			//获取输入流
			out = conn.getOutputStream();
			//写入参数
			out.write(param.getBytes("utf-8"));
			//刷新
			out.flush();
			//获取输出流
			in = conn.getInputStream();
			//构造reader对象读取流数据
			reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
			
			String res = null;
			//循环读取每行数据
			while((res = reader.readLine())!=null){
				buffer.append(res);
			}
			
		} catch (MalformedURLException e) {
			//打印错误信息
			e.printStackTrace();
			//打印错误日志
			logger.error(e.getMessage());
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return null;
		} finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(reader!=null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				conn.disconnect();
			}
			
		}
		return buffer.toString();
	}
	
	/*
	 * JSON类型GET请求
	 * 
	 */
	public static String doGetJSON(String requestUrl){
		return doGet(requestUrl,JSONTYPE);
	}
	/*
	 * GET请求
	 * 
	 */
	public static String doGet(String requestUrl){
		return doGet(requestUrl,null);
	}
	
	private static String doGet(String requestUrl,String type){
		HttpURLConnection conn = null;
		URL url = null;
		InputStream in = null;
		BufferedReader reader = null;
		OutputStream out = null;
		StringBuffer buffer = new StringBuffer();
		try {
			url = new URL(requestUrl);
			conn = (HttpURLConnection) url.openConnection();
			//指可以从这个连接中读取数据，默认为true
			conn.setDoInput(true);
			//指可以往这个连接写入数据，往服务器发送数据，默认为false
			conn.setDoOutput(true);
			//请求方法
			conn.setRequestMethod("GET");
			//连接超时时间 设置5分钟
			conn.setConnectTimeout(5*60*1000);
			//读取超时时间 设置5分钟
			conn.setReadTimeout(5*60*1000);
			//参数类型
			if(XMLTYPE.equals(type)){
				conn.addRequestProperty("Content-Type", "text/xml");
			}else if(JSONTYPE.equals(type)){
				conn.addRequestProperty("Content-Type", "application/json");
			}
			//获取输出流
			in = conn.getInputStream();
			//构造reader对象读取流数据
			reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
			String res = null;
			//循环读取每行数据
			while((res = reader.readLine())!=null){
				buffer.append(res);
			}
			
		} catch (MalformedURLException e) {
			//打印错误信息
			e.printStackTrace();
			//打印错误日志
			logger.error(e.getMessage());
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return null;
		} finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(reader!=null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				conn.disconnect();
			}
			
		}
		return buffer.toString();
	}
	
	private static String todo(String requestUrl,String param,String type,String method){
		HttpURLConnection conn = null;
		URL url = null;
		InputStream in = null;
		BufferedReader reader = null;
		OutputStream out = null;
		StringBuffer buffer = new StringBuffer();
		try {
			url = new URL(requestUrl);
			conn = (HttpURLConnection) url.openConnection();
			//指可以从这个连接中读取数据，默认为true
			conn.setDoInput(true);
			//指可以往这个连接写入数据，往服务器发送数据，默认为false
			conn.setDoOutput(true);
			//请求方法
			conn.setRequestMethod(method);
			//连接超时时间 设置5分钟
			conn.setConnectTimeout(5*60*1000);
			//读取超时时间 设置5分钟
			conn.setReadTimeout(5*60*1000);
			//参数类型
			if(XMLTYPE.equals(type)){
				conn.addRequestProperty("Content-Type", "text/xml");
			}else if(JSONTYPE.equals(type)){
				conn.addRequestProperty("Content-Type", "application/json");
			}
			
			//获取输入流
			out = conn.getOutputStream();
			//写入参数
			out.write(param.getBytes("utf-8"));
			//刷新
			out.flush();
			//获取输出流
			in = conn.getInputStream();
			//构造reader对象读取流数据
			reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
			
			String res = null;
			//循环读取每行数据
			while((res = reader.readLine())!=null){
				buffer.append(res);
			}
			
		} catch (MalformedURLException e) {
			//打印错误信息
			e.printStackTrace();
			//打印错误日志
			logger.error(e.getMessage());
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return null;
		} finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(reader!=null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				conn.disconnect();
			}
			
		}
		return buffer.toString();
	}
}
